/*
 *    Copyright (c) 2018-2025, 云集汇通 All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yunjihuitong.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 */

package com.yjht.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.Query;
import com.yjht.modules.sys.dao.SysLogDao;
import com.yjht.modules.sys.entity.SysLogEntity;
import com.yjht.modules.sys.service.SysLogService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Service("sysLogService")
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //按用户
        String username = (String) params.get("username");
        //按操作类型
        String operation = (String) params.get("operation");
        //operation = "保存角色";
        //按时间
        /*String aa = "2018-06-11 09:30:57";
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );*/
        Date startDate = (Date) params.get("startDate");
        Date endDate = (Date) params.get("endDate");
        /*Date endDate = null;
        try {
            endDate = sdf.parse(aa);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        EntityWrapper<SysLogEntity> ew = new EntityWrapper<SysLogEntity>();
        SysLogEntity condition = new SysLogEntity();
        ew.setEntity(condition);
        ew.like("username",username);
        ew.like("operation",operation);
        if(startDate != null){
            ew.ge("create_date",startDate);
        }
        if(endDate != null){
            ew.le("create_date",endDate);
        }



        Page<SysLogEntity> page = this.selectPage(new Query<SysLogEntity>(params).getPage(),ew);

        return new PageUtils(page);
    }

    /**
     * 异步插入日志
     *
     * @param sysLog 日志
     */
    @Async
    @Override
    public void asyncInsertLog(SysLogEntity sysLog) {
        insert(sysLog);
    }
}
